package com.peakmain.systemmoudle.presenter;

import android.annotation.SuppressLint;

import com.peakmain.systemmoudle.contract.SystemTreeContract;
import com.peakmain.wanandroid.netmodule.base.BasePresenter;
import com.peakmain.wanandroid.netmodule.bean.DataResponse;
import com.peakmain.wanandroid.netmodule.bean.system.SystemTreeBean;
import com.peakmain.wanandroid.netmodule.net.ApiService;
import com.peakmain.wanandroid.netmodule.net.RetrofitManager;
import com.peakmain.wanandroid.netmodule.net.RxSchedulers;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.functions.Consumer;

/**
 * author: peakmain
 * createdata：2019/7/16
 * mail: 2726449200@qq.com
 * desiption:
 */
public class SystemTreePresenter extends BasePresenter<SystemTreeContract.View> implements SystemTreeContract.Presenter{
    @Inject
    public SystemTreePresenter(){

    }
    @SuppressLint("CheckResult")
    @Override
    public void getSystemTree() {
        mView.showLoading();
        RetrofitManager.create(ApiService.class).getSystemTree()
                .compose(mView.<DataResponse<List<SystemTreeBean>>>bindToLife())
                .compose(RxSchedulers.<DataResponse<List<SystemTreeBean>>>applySchedulers())
                .subscribe(new Consumer<DataResponse<List<SystemTreeBean>>>() {
                    @Override
                    public void accept(DataResponse<List<SystemTreeBean>> dataResponse) throws Exception {
                        if (dataResponse.getErrorCode() == 0) {
                            mView.showSystemTreeSuccess(dataResponse.getData());
                        } else {
                            mView.showFaild(dataResponse.getErrorMessage());
                        }
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.showFaild("网络错误，请稍后重试");
                        mView.hideLoading();
                    }
                });
    }
}
